package main

import (
	"boke/blogpb"
	"context"
	"fmt"
	"github.com/rs/zerolog/log"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// 处理获取blog请求
func (s *server) ReadBlog(ctx context.Context, req *blogpb.ReadBlogRequest) (*blogpb.ReadBlogResponse, error) {
	fmt.Println("接收 Read blog request 请求")

	// 1. 获取请求ID
	blogID := req.GetBlog().Id

	// 2. 通过id查询
	bi := &blogItem{}
	err = db.Get(bi, select_sql, blogID)
	if err != nil {
		err = status.Errorf(codes.Internal, fmt.Sprintf("Internal error: %v", err))
		log.Error().Err(err).Msg("查询博客列表失败")
	}

	return &blogpb.ReadBlogResponse{
		Blog: &blogpb.Blog{
			Id:       bi.ID,
			AuthorId: bi.AuthorID,
			Content:  bi.Content,
			Title:    bi.Title,
		},
	}, nil
}

func dataToBlogPb(data *blogItem) *blogpb.Blog {
	return &blogpb.Blog{
		Id:       data.ID,
		AuthorId: data.AuthorID,
		Content:  data.Content,
		Title:    data.Title,
	}
}
